/* global LS */
require(["common", "eventManager", "coAjax", "queryString", "href", "hash_routie"], function(jSouper, eventManager, coAjax, QueryString, href, hash_routie) {
	var queryString = new QueryString(location.hash);
	/*
	 * 推荐者信息
	 */
	var recommender_id = queryString.get("rcid");
	if (recommender_id) {
		//将推荐者的信息保存起来，或者直接覆盖上一个推荐者，以最后一个推荐者为准
		LS.set("recommender_id", recommender_id);
	}
	App.set("$Cache.recommender_id", recommender_id);
	/*
	 * 二维码链接
	 */
	//定义默认链接
	App.set("$Cache.recommender_url", "http://" + location.host + location.pathname + "?id=" + queryString.get("id"))
	eventManager.is(App.get("loginer._id"), "getLoginer", function() {
		var user_id = App.get("loginer._id");
		App.set("$Cache.recommender_url", "http://" + location.host + location.pathname + "?id=" + queryString.get("id") + "&rcid=" + user_id)
	});
	var goods_id = queryString.get("id");
	//参数错误，返回首页
	if (!goods_id) {
		href.toMain();
	}
	coAjax.get(appConfig.goods.detail + goods_id, function(result) {
		jSouper.ready(function() {
			var goods_info = result.result;
			//处理数据格式
			goods_info.price = +goods_info.price || 0;
			goods_info.price_toFixed_2 = goods_info.price.toFixed(2);
			goods_info.fare = +goods_info.fare || 0;
			goods_info.fare_toFixed_2 = goods_info.fare.toFixed(2);
			App.set("goods_detail", goods_info);
			App.set("$Cache.goods_recommender_url", "http://" + location.host + "/catering.main.html#default/detail?id=" + App.get("goods_detail._id") + "&rcid=" + App.get("loginer._id"));
		});
	});

	/*
	 * 检验是否收藏了这个商品
	 */
	eventManager.is(App.get("loginer.collect"), "getLoginer", function() {
		var collect = App.get("loginer.collect") || [];
		_is_collect_goods(collect);
	});

	function _is_collect_goods(collect) {
		var result = false;
		if (jSouper.indexOf(collect, goods_id) !== -1) {
			result = true;
		}
		App.set("$Cache.goods_detail.is_collect", result);
	}
	App.set("$Event.goods.collect_toggle", function() {
		if (App.get("$Cache.goods_detail.is_collect")) {
			coAjax["delete"](appConfig.user.collect_remove, {
				goods_id: goods_id
			}, function(result) {
				alert("已经取消收藏");
				_is_collect_goods(result.result);
			}, function(errorCode, xhr, errorMsg) {
				alert("error", errorMsg)
			});
		} else {
			coAjax.post(appConfig.user.collect_add, {
				goods_id: goods_id
			}, function(result) {
				alert("success", "收藏成功");
				_is_collect_goods(result.result);
			}, function(errorCode, xhr, errorMsg) {
				alert("error", errorMsg)
			});
		}
	});
	// 购买
	function _get_cart_success(result) {
		var cart_detail = result.result;

		App.set("user.cart_detail", cart_detail);
		App.set("$Cache.cart.total_price", Model.Observer(function() {
			var cart_detail = App.get("user.cart_detail");
			var total_price = 0;
			cart_detail.forEach(function(cart) {
				cart.goods_info.forEach(function(goods_info) {
					total_price += goods_info.goods.price * goods_info.num;
				});
			});
			return total_price;
		}));
		alert("success", "添加成功")
	};

	function _get_cart_error(errorCode, xhr, errorMsg) {
		console.log("%c" + errorCode + ": " + errorMsg, "color:orange;font-size:14px; text-shadow: 0 1px 0 #ccc;");
		alert("error", errorMsg);
	};
	App.set("$Event.goods.add_to_cart", function(e) {
		App.set("$private", 'focus');
		var cart_item = {
			goods_id: App.get("goods_detail._id"),
			num: App.get("$Cache.goods_details.buy_number")
		};
		if (App.get("loginer")) {
			coAjax.post(appConfig.user.cart, cart_item, _get_cart_success, _get_cart_error);
		} else {
			var cache_cart = LS.get("$Cache.cart");
			try {
				cache_cart = JSON.parse(cache_cart);
			} catch (e) {
				console.error(e)
			}
			if (!(cache_cart instanceof Array)) {
				cache_cart = [];
			}
			cache_cart.push(cart_item)
		}
	});
});